package com.ruoyi.web.controller.lowcode;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.lowcode.domain.vo.FormModelFieldVo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.lowcode.domain.vo.LcFieldVo;
import com.ruoyi.lowcode.domain.bo.LcFieldBo;
import com.ruoyi.lowcode.service.ILcFieldService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 模型字段
 *
 * @author ruoyi
 * @date 2023-09-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/lowcode/field")
public class LcFieldController extends BaseController {

    private final ILcFieldService iLcFieldService;

    /**
     * 查询模型字段列表
     */
    @SaCheckPermission("lowcode:field:list")
    @GetMapping("/list")
    public TableDataInfo<LcFieldVo> list(LcFieldBo bo, PageQuery pageQuery) {
        return iLcFieldService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询模型字段列表
     */
    @SaCheckPermission("lowcode:field:list")
    @GetMapping("/queryList")
    public R<List<LcFieldVo>> queryList(LcFieldBo bo) {
        return R.ok(iLcFieldService.queryList(bo));
    }

    /**
     * 导出模型字段列表
     */
//    @SaCheckPermission("lowcode:field:export")
//    @Log(title = "模型字段", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(LcFieldBo bo, HttpServletResponse response) {
//        List<LcFieldVo> list = iLcFieldService.queryList(bo);
//        ExcelUtil.exportExcel(list, "模型字段", LcFieldVo.class, response);
//    }

    /**
     * 获取模型字段详细信息
     *
     * @param fieldId 主键
     */
    @SaCheckPermission("lowcode:field:query")
    @GetMapping("/{fieldId}")
    public R<LcFieldVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long fieldId) {
        return R.ok(iLcFieldService.queryById(fieldId));
    }

    /**
     * 新增模型字段
     */
    @SaCheckPermission("lowcode:field:add")
    @Log(title = "模型字段", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody LcFieldBo bo) {
        return toAjax(iLcFieldService.insertByBo(bo));
    }

    /**
     * 修改模型字段
     */
    @SaCheckPermission("lowcode:field:edit")
    @Log(title = "模型字段", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody LcFieldBo bo) {
        return toAjax(iLcFieldService.updateByBo(bo));
    }

    /**
     * 删除模型字段
     *
     * @param fieldIds 主键串
     */
    @SaCheckPermission("lowcode:field:remove")
    @Log(title = "模型字段", businessType = BusinessType.DELETE)
    @DeleteMapping("/{fieldIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] fieldIds) {
        return toAjax(iLcFieldService.deleteWithValidByIds(Arrays.asList(fieldIds), true));
    }
}
